<?php
namespace app\admin\controller;

use app\admin\model\ChatMsg;
use app\admin\model\Kf;
use plugin\Excel;

class UserInfo extends Common{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        /**
         * 参数说明
         * post.keyword 可根据该值获取 与name，company, phone匹配的回复信息
         * post.start_time 开始时间
         * post.end_time 结束时间
         */
        $input = input('post.');
        $input = array_merge(['keyword'=>""],$input);
        //生成session，方便导出excel时直接获取
        $this->searchSession($input);
        $UserInfoModel = new \app\admin\model\UserInfo();
        $list = $UserInfoModel->all($input);
        //客服列表
        $KfModel = new Kf();
        $Kf_list = $KfModel->all(['pageLimit'=>100]);
        $this->assign([
            'list' => $list['data'],
            'page' => $list['page'],
            'input'     => $input,
            'kf_list'   => $Kf_list['data']
        ]);
        return $this->fetch();
    }

    //把搜索条件储存session
    public function searchSession($input)
    {
        session('UserInfo_index_search',json_encode($input,JSON_UNESCAPED_UNICODE));
    }

    //获取之前搜索的条件
    public function getSearchSession()
    {
        $input =  session('UserInfo_index_search');
        return json_decode($input,true);
    }

    //ajax删除自动回复
    public function del()
    {
        /**
         * 参数说明
         * param.id
         */
        $input  = !empty(input('param.id')) ? [input('param.id')] : null;
        if(empty($input))
        {
            return ['error'=>1,'msg'=>'没有选中记录被删除'];
        }
        $UserInfoModel = new \app\admin\model\UserInfo();
        return $UserInfoModel->del($input);
    }

    //删除多个自动回复
    public function delAll()
    {
        /**
         * 参数说明
         * post.id[]
         */
        $input = input('post.');
        if(!isset($input['id']) || empty($input['id']))
        {
            $this->error("没有选中记录");
        }
        $UserInfoModel = new \app\admin\model\UserInfo();
        $result = $UserInfoModel->del($input['id']);
        $this->success($result['msg'],url('index'));
    }

    //编辑一个自动回复
    public function edit()
    {
        /**
         * 参数说明
         * param.id
         */
        $id = input('param.id');
        $UserInfoModel = new \app\admin\model\UserInfo();
        $data = $UserInfoModel->info(['id'=>$id]);
        if(!$data) $this->error('不存在该用户信息');
        $this->assign([
            'id' =>$id,
            'info'=>$data
        ]);
        return $this->fetch();
    }

    public function update()
    {
        /**
         * 参数说明
         * title 标题
         * content 自动回复内容
         */
        $input = ArrRemoveJs(input('post.'));
        $UserInfoModel = new \app\admin\model\UserInfo();
        $result = $UserInfoModel->edit($input);
        if($result['error'] != 0)
        {
            $this->error($result['msg']);
        }
        else
        {
            $this->success($result['msg'],url('index'));
        }
    }

    //导出excel文件
    public function export()
    {
        $input = $this->getSearchSession();
        $UserInfoModel = new \app\admin\model\UserInfo();
        $list = $UserInfoModel->all($input);
        $list = json_decode(json_encode($list['data'],JSON_UNESCAPED_UNICODE),true);
        $export = [
            'file_name' => date('Y-m-d')."_user_chatlog",
            'ex_title'  => "用户信息 ",
            'data'      => $list['data'],
            'ex_th'     =>['add_time'=>"日期",'name'=>"用户名",'phone'=>"联系方式","company"=>"公司","remark"=>"备注"],
            'data_init' => function($key,$value){
                if($key == 'add_time'){
                    $value = date('Y-m-d',$value);
                }
                return strip_tags($value);
            }
        ];
        $str = '[{"shop_name":"历宏装备","video":"https:\/\/player.youku.com\/player.php\/sid\/XMjczMTM0MjU0NA==\/v.swf"},{"shop_name":"利洋机械","video":"..\/seller_imgs\/seller_video\/seller_video30.mp4"},{"shop_name":"众工机械","video":"..\/seller_imgs\/seller_video\/seller_video55.mp4"},{"shop_name":"桦桦机械","video":"..\/seller_imgs\/seller_video\/seller_video105.mp4"},{"shop_name":"正昌工具","video":"https:\/\/imgcache.qq.com\/tencentvideo_v1\/playerv3\/TPout.swf?max_age=86400&v=20161117&vid=r0504s7dzwq&auto=0"},{"shop_name":"铨智动木工机械","video":"..\/seller_imgs\/seller_video\/seller_video168.mp4"},{"shop_name":"极宇机械","video":"http:\/\/v.youku.com\/v_show\/id_XMTY1Mzc4NDQyMA==.html?from=y1.2-1-176.4.1-1.1-1-2-0-0%26source%3Dautoclick"},{"shop_name":"豪德机械","video":"..\/seller_imgs\/seller_video\/seller_video214.mp4"},{"shop_name":"新俊数控","video":"..\/seller_imgs\/seller_video\/seller_video241.flv"},{"shop_name":"林丰机械","video":"..\/seller_imgs\/seller_video\/seller_video244.mp4"},{"shop_name":"郑太机械","video":"https:\/\/static.video.qq.com\/TPout.swf?vid=n0137j7jfne&auto=0"},{"shop_name":"弘历伟业","video":"https:\/\/player.youku.com\/player.php\/sid\/XMTgzMjIzNTU2NA==\/v.swf"},{"shop_name":"亮齿传动","video":"https:\/\/player.youku.com\/player.php\/sid\/XMTg0NDE0NTY4MA==\/v.swf"},{"shop_name":"华泽机械","video":"https:\/\/player.youku.com\/player.php\/sid\/XMTg1MjY5OTA0NA==\/v.swf"},{"shop_name":"金泓宇机械","video":"..\/seller_imgs\/seller_video\/seller_video428.mp4"},{"shop_name":"广特机械","video":"https:\/\/player.youku.com\/player.php\/sid\/XMjUxNDMxOTk1Ng==\/v.swf"},{"shop_name":"测试店铺","video":"http:\/\/imgcache.qq.com\/tencentvideo_v1\/playerv3\/TPout.swf?max_age=86400&v=20161117&vid=r0504s7dzwq&auto=0"},{"shop_name":"品瑞良机械","video":"..\/seller_imgs\/seller_video\/seller_video470.mp4"},{"shop_name":"豪田机械","video":"..\/seller_imgs\/seller_video\/seller_video517.mp4"}]';
        $export = [
            'file_name' => "商家视频列表",
            'ex_ttile'  => "视频列表",
            'data'      => json_decode($str,true),
            'ex_th'     => ['shop_name'=>'商家名称','video'=>"视频地址"],
            'data_init' => function($key,$value)
            {
                //初始化video链接
                if($key != 'video')
                {
                    return $value;
                }
                if(strpos($value,"https://") === false && strpos($value,"http://") === false && strpos($value,".com/") === false)
                {
                    $value = "https://www.jxcat.com/".trim($value,"./");
                }
                return $value;
            }

        ];
        $excel = new Excel($export);
        $excel->export();
    }

    //聊天数据
    public function chat_log()
    {
        /**
         * 参数说明
         * param.id 聊天用户的client_id
         * param.id 聊天客服的kf_id
         */
        $input = input('get.');
        $KfModel = new Kf();
        $info = $KfModel->info(['kf_id'=>$input['tid']]);
        if(empty($info))
        {
            $this->error('您选择的客服不存在');
        }
        $input['tid'] = $info['client_id'];
        $chatLog = new ChatMsg();
        $data = $chatLog->getlog($this->EncUserid($input['fid']),$this->EncUserid($input['tid']));
        $export = [
            'file_name' => date('Y-m-d')."_user_chatlog",
            'ex_title'  => "聊天记录",
            'data'   => $data,
            'ex_th'     => ['nickname'=>"名称",'add_time'=>"时间",'content'=>"内容",'content_type'=>"内容类型"],
            'data_init' => function($key,$value){
                switch($key)
                {
                    case "content_type":
                        switch($value)
                        {
                            case "text" :
                                $value = "文本消息";
                                break;
                            case "photo" :
                                $value = "图片消息";
                                break;
                            case "OffLineText":
                                $value = "不在线自动推送的消息";
                                break;
                        }
                        break;
                }
                return strip_tags($value);
            }
        ];

        $excel = new Excel($export);
        $excel->export();
    }

}